<?php
class Core_Model_Facebook implements Core_Model_Mainable {
    
    private $facebook;
    
    public function __construct() {
        require_once WEB_ROOT_FOLDER.'facebookoauth/facebook.php';
        $this->facebook = new Facebook(array(
            'appId' => FACEBOOK_API_ID,
            'secret' => FACEBOOK_SECRET
        ));
    }
    
    public function main(array $variables = null) {
        
        if(isset($_SESSION['fb_223995551058125_state'])){
            $_SESSION['facebook_user'] = $this->get_facebook_user();
            $_SESSION['facebook_user_profile'] = $this->get_facebook_user_profile();
            //var_dump($variables);
            return $variables;
        }
        else {
           echo '<meta http-equiv="Refresh" content="0;url='.$this->get_facebook_login_url().'">'; 
        }
        //var_dump($variables);
        //die();
        return $variables;
    }
    
    
    private function get_facebook_user_profile(){
        try {
            return $this->facebook->api('/me');
        } catch (FacebookApiException $ex) {
            error_log($ex);
            return null;
        }
    }
    
    private function get_facebook_user(){
        return $this->facebook->getUser();
    }
    
    private function get_facebook_logout_url(){
        return $this->facebook->getLogoutUrl();
    }
    
    private function get_facebook_login_url(){
        return $this->facebook->getLoginUrl(array('scope' => 'email'));
    }
    

}
